package com.A21MobileSales;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.A21.Adapter.CustomerListAdapter;
import com.A21.Object.MSTCustomer;
import com.A21MobileSales.DataBase.DatabaseHelper;

public class CustomerSelection extends Activity {

 
	ListView lvCustomer;
	EditText edtSearchCustomer;

	DatabaseHelper ObjDBHelper;

	ArrayList<MSTCustomer> lstCustomers;
	String KEY_CName = "CName";
	String KEY_SalesPerson = "SalesPerson";
	String KEY_CCode = "CCode";
	AlertDialog.Builder _Alert;
	Button btnDelSearch;
	CustomerListAdapter customerListAdapter;

	Button btn_Home;
	ImageButton btn_back;
	String TrnType;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_selection);
		try {
			if (getIntent().hasExtra("TrnType")) {
				TrnType = getIntent().getStringExtra("TrnType");
			}
			// ListView for Customer Select
			lvCustomer = (ListView) findViewById(R.id.CLS_ListviewCustomerSelect);
			btn_Home = (Button) findViewById(R.id.btn_home);
			btn_Home.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent _intent = new Intent();
					_intent.setClass(CustomerSelection.this, HomeScreen.class);
					startActivity(_intent);
				}
			});

			btn_back = (ImageButton) findViewById(R.id.btn_back);
			btn_back.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					GoBack();
				}
			});

			ObjDBHelper = new DatabaseHelper(this);  
			btnDelSearch = (Button) findViewById(R.id.btn_del_search);
			btnDelSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					edtSearchCustomer.setText(""); 
				}
			});

			
			lvCustomer.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> adapter, View v,
						int Pos, long Id) {
					try {
						Bundle b = new Bundle();
						b.putString("Code", lstCustomers.get(Pos).Code);
						b.putString("CName", lstCustomers.get(Pos).CName);
						b.putString("TrnType", TrnType);
						Intent _intent = new Intent();
						_intent.putExtras(b);
						_intent.setClass(CustomerSelection.this,
								OrderSummary.class);
						startActivity(_intent);
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});

			edtSearchCustomer = (EditText) findViewById(R.id.edtSearchCustomer);
			edtSearchCustomer.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					String text = s.toString().toLowerCase(Locale.getDefault());
					customerListAdapter.filter(text);  
				}
			});
			new CustomerTask().execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	@Override
	public void onBackPressed() {
		GoBack();
	}
	
	private void GoBack(){
		Bundle b = new Bundle();
		Intent _intent = new Intent();
		_intent.putExtras(b);
		_intent.setClass(CustomerSelection.this, HomeScreen.class);
		startActivity(_intent); 
	}
	
	 public void bindData() { 
		   customerListAdapter = new CustomerListAdapter(
					CustomerSelection.this, lstCustomers);
		     customerListAdapter.notifyDataSetChanged();
			lvCustomer.setAdapter(customerListAdapter); 
			
		}

		private class CustomerTask extends AsyncTask<String, Void, Void> {

			private ProgressDialog dialog = new ProgressDialog(CustomerSelection.this);
			String StrMsg = null;

			protected void onPreExecute() {
				this.dialog.setMessage("Loading...");
				this.dialog.setCanceledOnTouchOutside(false);
				this.dialog.show();

			}

			protected Void doInBackground(final String... Params) { 
				try { 
					lstCustomers = ObjDBHelper.GetAllCustomers(false);
				} catch (Exception ex) {
					ex.printStackTrace();
				}

				return null;
			}

			protected void onPostExecute(final Void unused) {
				if (this.dialog.isShowing()) {
					this.dialog.dismiss();
					this.dialog = null;
				}
				try { 
						bindData(); 
				} catch (Exception ex) {

					ex.printStackTrace();

				}

			}

		}
}